/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client.user;



import java.util.HashMap;
import java.util.Map;

import net.bioteam.appweb.gwt.apps.client.StaticHolder;
import net.bioteam.appweb.gwt.apps.client.Util.ApplicationCallBack;
import net.bioteam.appweb.gwt.apps.client.Util.Util;
import net.bioteam.appweb.gwt.apps.clientandserver.LoginProxy;
import net.bioteam.appweb.gwt.apps.clientandserver.LoginProxyAsync;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.FormErrorOrientation;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.validator.CustomValidator;
import com.smartgwt.client.widgets.form.validator.LengthRangeValidator;
import com.smartgwt.client.widgets.form.validator.RegExpValidator;
import com.smartgwt.client.widgets.form.validator.Validator;
import com.smartgwt.client.widgets.layout.HLayout;

public class UserRegisterWindow extends Window
{


	public UserRegisterWindow()
	{
		
		setWidth(550);
		setHeight(400);
		setTitle("Register");
		setShowMinimizeButton(false);
		setIsModal(true);
		centerInPage();
		//setAutoSize(true);
		setShowCloseButton(true);
		
		addCloseClickHandler(new CloseClickHandler(){

			public void onCloseClick(CloseClientEvent event)
			{
				hide();
				destroy();
			}});
		

		final Label messageLabel=new Label();
		messageLabel.setStyleName("red");
		messageLabel.setHeight(40);
		messageLabel.setWidth100();
		messageLabel.setAlign(Alignment.CENTER);
		
		final DynamicForm form = new DynamicForm();
		// form.setBorder("2px solid red");
		
		form.setWidth100();
		form.setHeight(250);
		form.setOverflow(Overflow.AUTO);
		form.setColWidths(150, 100);
		form.setCellBorder(5);
		form.setNumCols(2);

		form.setShowInlineErrors(true);
		form.setShowErrorText(true);
		form.setErrorOrientation(FormErrorOrientation.TOP);
		form.setRequiredTitleSuffix("<em style='color:red;'>***</em>");
		
		TextItem email = new TextItem("email");
		email.setTitle("Your email");
		email.setRequired(true);
		RegExpValidator regV=new RegExpValidator();
		regV.setExpression(".+@.+");
		regV.setErrorMessage("invalid email address");
		email.setValidators(regV);
		
		
		TextItem username=new TextItem("username");
		Validator validator=new CustomValidator()
		{

			@Override
			protected boolean condition(Object value)
			{
				String str=value.toString();
				if (str.length()<6)
				{
					setErrorMessage("must have 6 characters at minimal");
					return false;
				}
				if (!str.matches("^[a-zA-Z].+"))
				{
					setErrorMessage("First character has to be a letter");
					return false;
				}
				if (!str.matches("(\\w|\\d)+"))
				{
					setErrorMessage("all characters have to be letter or number");
					return false;
				}
				return true;
			}
		};
		username.setValidators(validator);
		username.setRequired(true);
		
		TextItem password = new TextItem("password");
		LengthRangeValidator lrv=new LengthRangeValidator();
		//lrv.setMin("6");
		lrv.setMin(new Integer(6));
		password.setValidators(lrv);
		password.setRequired(true);
		
		form.setItems(email, username, password);
		
		HLayout hb=new HLayout();
		hb.setHeight(30);
		hb.setWidth(300);
		//hb.setBackgroundColor("yellow");
		hb.setLayoutAlign(Alignment.CENTER);
		IButton b=new IButton("Ok");
		b.setWidth(80);
		b.addClickHandler(new ClickHandler(){

			public void onClick(ClickEvent event)
			{
				ApplicationCallBack<String> callback = new ApplicationCallBack<String>()
				{

					public void onFailureAfter(Throwable caught)
					{
						StaticHolder.setMessage(messageLabel, caught.getMessage(), 5);
						//messageLabel.setContents(caught.getMessage());
					}

					public void onSuccessAfter(String u)
					{
						
						messageLabel.setContents("Email is sent to you for verrification. You are ready to login in");
						UserRegisterWindow.this.hide();
						UserRegisterWindow.this.destroy();
					}

					public void execute()
					{
						String email=form.getValueAsString("email");
						String username=form.getValueAsString("username");
						String password=form.getValueAsString("password");
						String hash=Util.mymd5(email+"_"+username);
						
						Map<String, String> values=new HashMap<String, String>();
						values.put("email", email);
						values.put("username", username);
						values.put("password", password);
						values.put("hash", hash);
						LoginProxyAsync login=LoginProxy.Util.getInstance();
						login.register(values, this);				
					}

				};
				callback.execute();
				
			}});
		
		
		IButton c=new IButton("Cancel");
		c.addClickHandler(new ClickHandler(){

			public void onClick(ClickEvent event)
			{
				UserRegisterWindow.this.hide();
				UserRegisterWindow.this.destroy();
			}});
		c.setWidth(80);
		hb.addMember(b);
		hb.addMember(c);
		
		
		
		addItem(messageLabel);
		addItem(form);
		addItem(hb);

	}

}
